/**
 * @author zly
 */
(function($){

	$(document).ready(function(){
		validateForm();
	});
	
	function validateForm() {
		// 用户名验证   
		jQuery.validator.addMethod(
			"username",
			function(value,element) {   
			  	return /^[a-z0-9_]*$/i.test(element.value);   
			}, 
			"只能包含字母数字和下划线"
		);
		
		jQuery.validator.addMethod(
			"goodtel",
			function(value,element){
				return /^\d{3,4}[-]\d{7,8}$/.test(element.value);  	
			},
			"格式为010-1234567"
		);
		
		$("#register form.form_reg").validate({
			rules: {
				"user[username]": {
					required: true,
					username: true,
					rangelength: [6,20],  
					remote: {
        				url: "/admin/users/user_exist",
        				type: "get",
        				data: {
          						username: function() {
            						return $("#user_username").val();
          						}
        				}
					}
				},
				"user[passwd]": {
					required: true,
					rangelength: [6,20]
				},
				"user[passwd_confirmation]": {
					required: true,
					rangelength: [6,20],
					equalTo: "#user_passwd"
				},
				"user[name]": "required",
				"user[gender]": "required",
				"user[company]": "required",
				"user[captcha]": {
					required: true,
					remote: {
        				url: "/admin/users/correct_captcha",
        				type: "get",
        				data: {
          						simple_captcha: function() {
            						return $("#user_captcha").val();
          						},
								simple_captcha_key: function(){
									return $("#user_captcha_key").val();
								}
        				}
					}
				},
				"user[tel]": {
					required: true,
					goodtel: true
				},
				"user[email]": {
					required: true,
					email: true,  
					remote: {
        				url: "/admin/users/user_exist",
        				type: "get",
        				data: {
          						email: function() {
            						return $("#user_email").val();
          						}
        				}
					}
				}
			},
			messages: {
				"user[username]": {
					required: "请输入用户名",
					username: "只能包含字母数字和下划线",
					rangelength: "用户名长度6-20",
					remote: "用户名已存在"
				},
				"user[passwd]": {
					required: "请输入密码",
					rangelength: "密码长度6-20"
				},
				"user[passwd_confirmation]": {
					required: "请输入确认密码",
					rangelength: "密码长度6-20",
					equalTo: "与上面的密码不一致"
				},
				"user[tel]": {
					required: "请输入固定电话",
					goodtel: "格式为010-1234567"
				},
				"user[name]": "请输入您的名字",
				"user[gender]": "请选择您的性别",
				"user[company]": "请输入公司名称",
				"user[captcha]": {
					required: "请输入验证码",
					remote: "验证码不正确"
				},
				"user[email]": {
					required: "请输入您的电子邮件",
					email: "请输入有效的电子邮件",
					remote: "邮箱地址已存在"
				}
			},
			errorPlacement: function(error, element) {
				error.appendTo( element.parent() );
			},
			success: function(label) {
 			// set &nbsp; as text for IE
 			label.html("&nbsp;").addClass("checked");
 			},
			submitHandler: function(form) {
				showLoading();
				form.submit();
			}
		});	
	}

})(jQuery);